import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import getters from './getters'
import user from './user/index'
import work from './work/index'

export default new Vuex.Store({
    state:{
        str:'全局str',
        globalList:[
            {title:'苹果',},
            {title:'香蕉'},
            {title:'榴莲'},
        ],
        message:22,
        // list:JSON.parse(localStorage.getItem('regist'),
        regist:[
            {
                name:'qwq',
                pass:'ew'
            }
        ],
        login:JSON.parse(localStorage.getItem('login')) || '',//前面不成立取到的值为null的话就执行后面这句空
    },
    getters,//过滤
    work,
    actions:{//计算属性
        action1( { commit } ,value) {//解构的方法
            //调用mutation方法传值
            console.log('action1-value',value)
            commit('changeList',value)
        }
    },
    mutations:{//定义修改state的方法
        updateMessage(state,value){
            console.log('state.message',state.message)
            console.log('message',value)
            state.message = value
        },
        setName (state, value ){
            state.str = value
        },
        changeList(state,value){//第一个参数永远指向state状态固定的
            console.log('value',value)
            state.globalList = value
            // state.globalList = value.data
        },//调用是store.commit('调用的名字‘,'需要传的参数)
        regist(state,value){//第一个参数永远指向state状态固定的
            state.regist = JSON.parse(localStorage.getItem('regist'))
            state.regist.namme = value.text
            state.regist.pass = value.pass
            console.log(state)
            state.regist = []
            // console.log(state.regist.pass)
            if(state.regist[0].name === value.text && state.regist[0].pass === value.pass){
                console.log('全等',state.regist.name)
            }else{
                // console.log(state.regist[0].name)
                // console.log(state.regist[0].pass)
                // console.log(value.text)
                // console.log(value.pass)
                this.state.regist.forEach(item => {
                        console.log(item.name)
                });
            }
        },
        loginUs(state,value){
            state.login = value
            localStorage.setItem('login',JSON.stringify(state.login))
        }
    },
    modules: {//模块声明
        user
    }
})//相当于在全局声明 所以在页面上使用直接通过$store.state.str来使用
